home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / GRIDS / STRGRDEX / STRGRDEX.PAS < prev    next >
Pascal/Delphi Source File  |  1996-10-22  |  9KB  |  308 lines

  1. unit strgrdex;
  2.  
  3. // Written By Tom Lee , Taiwan , Republic of China
  4. // Ver 1.02 Last modify Date OCT 22 1996
  5. // Freeware Component For Delphi 2.0x
  6. // E-Mail : tom@libra.aaa.hinet.net
  7. // Home Page : http://www.aaa.hinet.net/delphi
  8.  
  9.  
  10. interface
  11.  
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   Grids;
  15.  
  16. type
  17.   TStringGridEx = class(TStringGrid)
  18.   private
  19.     { Private declarations }
  20.     FCTL3D:boolean;
  21.     FLastRow:integer;
  22.     FMouseDownRow:integer;
  23.     FMultiSelect:Boolean;
  24.     FOriginRowCount:integer;
  25.     FSelectedColor:TColor;
  26.     FSelectedTextColor:TColor;
  27.     FSelectRows:TStrings;
  28.     procedure DeSelectAll;
  29.     procedure SetCTL3D(value:Boolean);
  30.     procedure SetMultiSelect(value:Boolean);
  31.     procedure SetSelectedColor(value:TColor);
  32.     procedure SetSelectedTextColor(value:TColor);
  33.   protected
  34.     { Protected declarations }
  35.      procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
  36.      procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  37.      procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
  38.      procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
  39.   public
  40.     { Public declarations }
  41.     constructor Create(AOwner: TComponent); override;
  42.     destructor Destroy; override;
  43.     function GetSelectRow(RowValue:integer):Boolean;
  44.     function GetSelectRowCount:integer;
  45.     procedure SetSelectRow(RowValue:integer;Selected:Boolean);
  46.   published
  47.     { Published declarations }
  48.     property CTL3D : boolean read FCTL3D write SetCTL3D default false;
  49.     property SelectedColor:TColor read FSelectedColor write SetSelectedColor default clHighLight;
  50.     property SelectedTextColor:TColor read FSelectedTextColor write SetSelectedTextColor default clHighLightText;
  51.     property MultiSelect :Boolean read FMultiSelect write SetMultiSelect default false;
  52.   end;
  53.  
  54. procedure Register;
  55.  
  56. implementation
  57.  
  58. procedure Register;
  59. begin
  60.   RegisterComponents('Samples', [TStringGridEx]);
  61. end;
  62.  
  63. constructor TStringGridEx.Create(AOwner: TComponent);
  64. var
  65.    idx:integer;
  66. begin
  67.      inherited Create(AOwner);
  68.      options:=[goDrawFocusSelected,goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRowSelect];
  69.      FMultiSelect:=False;
  70.      FOriginRowCount:=RowCount;
  71.      FSelectRows:=TStringList.Create;
  72.      for idx:=0 to RowCount - 1 do FSelectRows.Add('N');
  73.      FSelectedColor:=clHighLight;
  74.      FSelectedTextColor:=clHighLightText;
  75.      FLastRow:=FixedRows;
  76.      FSelectRows[FLastRow]:='Y';
  77.      FCtl3D:=False;
  78. end;
  79.  
  80. destructor TStringGridEx.Destroy;
  81. begin
  82.      FSelectRows.Free;
  83.      inherited Destroy;
  84. end;
  85.  
  86. procedure TStringGridEx.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
  87. var
  88.    idx:integer;
  89. begin
  90.      if FOriginRowCount>RowCount then
  91.      begin
  92.           FSelectRows.Clear;
  93.           for idx:=0 to RowCount - 1 do FSelectRows.Add('N');
  94.           FOriginRowCount:=RowCount;
  95.      end;
  96.  
  97.      if FOriginRowCount<RowCount then
  98.      begin
  99.           for idx:=FOriginRowCount to RowCount - 1 do FSelectRows.Add('N');
  100.           FOriginRowCount:=RowCount;
  101.      end;
  102.  
  103.      if (FSelectRows[ARow]='Y') and (ACol >= FixedCols) and (ARow >= FixedRows) then
  104.      begin
  105.           Canvas.Font.Color:=FSelectedTextColor;
  106.           Canvas.Brush.color:=FSelectedColor;
  107.           Canvas.FillRect(ARect);
  108.      end;
  109.  
  110.      inherited DrawCell(ACol, ARow, ARect, AState);
  111.  
  112.      if (FCTL3D=True) and ([goVertLine,goHorzLine] * Options = [goVertLine,goHorzLine]) then
  113.      with ARect do
  114.      begin
  115.           Canvas.Pen.Color := clHighLightText;
  116.           Canvas.PolyLine([Point(Left, Bottom - 1), Point(Left, Top), Point(Right, Top)]);
  117.      end;
  118.  
  119. end;
  120.  
  121. procedure TStringGridEx.SetSelectedColor(value:TColor);
  122. begin
  123.      if value <> FSelectedColor then
  124.      begin
  125.           FSelectedColor:=value;
  126.           invalidate;
  127.      end;
  128. end;
  129.  
  130. procedure TStringGridEx.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  131. var
  132.    Arow,Acol : longint;
  133. begin
  134.      if FMultiSelect then
  135.      begin
  136.           MouseToCell(x,y,Acol,Arow);
  137.           FMouseDownRow:=Arow;
  138.      end;
  139.      inherited MouseDown(Button,Shift,X,Y);
  140. end;
  141.  
  142. procedure TStringGridEx.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  143. var
  144.      Arow,Acol : longint;
  145.      idx : integer;
  146. begin
  147.      MouseToCell(x,y,Acol,Arow);
  148.      if Arow<0 then Arow:=RowCount-1;
  149.  
  150.      if FMultiSelect then
  151.      begin // Multi-Select
  152.  
  153.           if Shift=[] then
  154.           begin
  155.                DeSelectAll;
  156.                if FMouseDownRow=ARow then
  157.                begin
  158.                     FSelectRows[ARow]:='Y';
  159.                     FLastRow:=ARow;
  160.                end
  161.                else
  162.                begin
  163.                     if ARow>FMouseDownRow then
  164.                         for Idx:= FMouseDownRow to ARow do FSelectRows[idx]:='Y'
  165.                     else
  166.                         for Idx:= FMouseDownRow downto ARow do FSelectRows[idx]:='Y';
  167.                end;
  168.           end;
  169.  
  170.           if  Shift=[ssShift] then
  171.           begin
  172.                DeSelectAll;
  173.                if ARow>FLastRow then
  174.                    for Idx:= FLastRow to ARow do FSelectRows[idx]:='Y'
  175.                else
  176.                    for Idx:= FLastRow downto ARow do FSelectRows[idx]:='Y';
  177.           end;
  178.  
  179.           if Shift=[ssCtrl] then
  180.           begin
  181.                if FSelectRows[ARow]='Y' then
  182.                begin
  183.                     FSelectRows[ARow]:='N';
  184.                     Invalidate;
  185.                end
  186.                else
  187.                begin
  188.                     FLastRow:=ARow;
  189.                     FSelectRows[ARow]:='Y';
  190.                end;
  191.           end;
  192.      end
  193.      else // Single Select
  194.      begin
  195.           FSelectRows[FLastRow]:='N';
  196.           FSelectRows[ARow]:='Y';
  197.           FLastRow:=ARow;
  198.      end;
  199.  
  200.      Invalidate;
  201.      inherited MouseUp(Button,Shift,X,Y);
  202. end;
  203.  
  204. procedure TStringGridEx.KeyUp(var Key: Word; Shift: TShiftState);
  205. var
  206.    Idx:integer;
  207. begin
  208.      if Shift = [] then
  209.      begin
  210.           if (Key=VK_HOME) or(Key=VK_END) or (Key=VK_PRIOR) or (Key=VK_NEXT)
  211.           or(Key=VK_UP) or (Key=VK_DOWN) or (Key=VK_LEFT) or (Key=VK_RIGHT)then
  212.           begin
  213.                DeSelectAll;
  214.                FSelectRows[Row]:='Y';
  215.                FLastRow:=Row;
  216.                Invalidate;
  217.           end;
  218.      end;
  219.  
  220.      if FMultiSelect then
  221.      begin
  222.           if Shift = [ssShift] then
  223.           begin
  224.                if (Key=VK_HOME) or(Key=VK_END) or (Key=VK_PRIOR) or (Key=VK_NEXT)
  225.                or(Key=VK_UP) or (Key=VK_DOWN) or (Key=VK_LEFT) or (Key=VK_RIGHT)then
  226.                begin
  227.                     if Row>FLastRow then
  228.                         for Idx:= FLastRow to Row do FSelectRows[idx]:='Y'
  229.                     else
  230.                         for Idx:= FLastRow downto Row do FSelectRows[idx]:='Y';
  231.  
  232.                     FLastRow:=Row;
  233.                     Invalidate;
  234.                end;
  235.           end;
  236.      end;
  237.  
  238.      inherited KeyUp(Key,Shift);
  239. end;
  240.  
  241. function TStringGridEx.GetSelectRow(RowValue:integer):Boolean;
  242. var
  243.    ret:Boolean;
  244. begin
  245.      if FSelectRows[RowValue]='Y' then
  246.          ret:=True
  247.      else
  248.          ret:=False;
  249.  
  250.      result:=ret;
  251. end;
  252.  
  253. procedure TStringGridEx.SetSelectRow(RowValue:integer;Selected:Boolean);
  254. begin
  255.      if Selected then
  256.         FSelectRows[RowValue]:='Y'
  257.      else
  258.         FSelectRows[RowValue]:='N';
  259.  
  260.      Invalidate;
  261. end;
  262.  
  263. procedure TStringGridEx.DeSelectAll;
  264. var
  265.    idx:integer;
  266. begin
  267.      for idx:=0 to FSelectRows.Count-1 do
  268.          FSelectRows[idx]:='N';
  269. end;
  270.  
  271. procedure TStringGridEx.SetMultiSelect(value:Boolean);
  272. begin
  273.      if value<>FMultiSelect then
  274.      begin
  275.           FMultiSelect:=value;
  276.      end;
  277. end;
  278.  
  279. function TStringGridEx.GetSelectRowCount:integer;
  280. var
  281.    idx,cnt:integer;
  282. begin
  283.      cnt:=0;
  284.      for idx:=0 to FSelectRows.Count-1 do
  285.       if FSelectRows[idx]='Y' then inc(Cnt);
  286.      result:=cnt;
  287. end;
  288.  
  289. procedure TStringGridEx.SetSelectedTextColor(value:TColor);
  290. begin
  291.      if value <> FSelectedTextColor then
  292.      begin
  293.           FSelectedTextColor:=value;
  294.           invalidate;
  295.      end;
  296. end;
  297.  
  298. procedure TStringGridEx.SetCtl3D(value:Boolean);
  299. begin
  300.      if FCTL3D<>Value then
  301.      begin
  302.           FCTL3D:=Value;
  303.           Invalidate;
  304.      end;
  305. end;
  306.  
  307. end.
  308.